/*
 * GUI.java
 *
 * Created on 2 grudzień 2007, 15:23
 */
package pl.cezary.butler.walili.client.gui;

import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import pl.cezary.butler.walili.common.TaskStatus;
import pl.cezary.butler.walili.common.Task;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Timer;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import pl.cezary.butler.walili.client.*;

/**
 *
 * @author  Przemysław Bałdyga, Cezary Butler
 */
public class GUI extends javax.swing.JFrame implements UserDataSource {

    private static final ResourceBundle rb = ResourceBundle.getBundle("gui");        
    private Timer perfTimer;   

 
    /** Creates new form GUI */    
    public GUI() {
        initComponents();
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        logowanie1.jButtonZaloguj.setActionCommand((logowanie1.jCheckBoxRejestrowac.isSelected()) ? LoginControler.SIGNIN : LoginControler.LOGIN);
        logowanie1.jButtonWyloguj.setActionCommand(LoginControler.LOGOUT);
        zadania1.jButtonZadanie.setActionCommand(TaskControler.SEND);
        zapytanie1.jButtonZapytanie.setActionCommand(QueryControler.SEND);
        perfTimer = TrafficUpdateProgressbarAction.scheduleTrafficLogging(1000, trafficBar);
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                setVisible(true);

            }
        });    
    }        

    public void setCredentialsVisibility(final boolean visible) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                logowanie1.setVisible(visible);
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        logowanie1 = new pl.cezary.butler.walili.client.gui.LogowanieGUI();
        logowanie1.setVisible(false);
        zadania1 = new pl.cezary.butler.walili.client.gui.ZadaniaGUI();
        zapytanie1 = new pl.cezary.butler.walili.client.gui.ZapytanieGUI();
        trafficBar = new javax.swing.JProgressBar();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Walili - Klient ");
        setBounds(new java.awt.Rectangle(0, 0, 0, 0));
        setMinimumSize(new java.awt.Dimension(500, 450));
        setName("mainFrame"); // NOI18N
        getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.PAGE_AXIS));

        logowanie1.setMaximumSize(new java.awt.Dimension(2147483647, 132));
        getContentPane().add(logowanie1);

        zadania1.setMaximumSize(new java.awt.Dimension(2147483647, 80));
        getContentPane().add(zadania1);

        zapytanie1.setMaximumSize(new java.awt.Dimension(2147483647, 2147483647));
        getContentPane().add(zapytanie1);

        trafficBar.setDoubleBuffered(true);
        getContentPane().add(trafficBar);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    /**
     * @param args the command line arguments
     */
    /*public static void main(String args[]) {
    
    }*/
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private pl.cezary.butler.walili.client.gui.LogowanieGUI logowanie1;
	private javax.swing.JProgressBar trafficBar;
    private pl.cezary.butler.walili.client.gui.ZadaniaGUI zadania1;
    private pl.cezary.butler.walili.client.gui.ZapytanieGUI zapytanie1;
    // End of variables declaration//GEN-END:variables

    @Override
    public String getHost() {
        return logowanie1.jTextFieldHost.getText();
    }

    @Override
    public String getPassword() {
        return new String(logowanie1.jPasswordFieldHaslo.getPassword());
    }

    @Override
    public String getLogin() {
        return logowanie1.jTextFieldLogin.getText();
    }

    @Override
    public String getZadanieCel() {
        return zadania1.jTextFieldZadanie.getText();
    }

    @Override
    public String getQueryTarget() {
        return zapytanie1.getQueryTarget();
    }

    public boolean getCzyRejestrowac() {
        if (logowanie1.jCheckBoxRejestrowac.getSelectedObjects() == null) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public String getQueryLocation() {
        return zapytanie1.getQueryLocation();
    }
//TODO nie wiem jaki stan przyjąc za dowolny? //null bedzie dobry

    @Override
    public TaskStatus getQueryStatus() {
        return getQueryStatus();
    }

    @Override
    public void enableLogowanie(final boolean stan) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                logowanie1.jButtonWyloguj.setEnabled(!stan);
                logowanie1.jButtonZaloguj.setEnabled(stan);
                logowanie1.jCheckBoxRejestrowac.setEnabled(stan);
                logowanie1.jPasswordFieldHaslo.setEditable(stan);
                logowanie1.jTextFieldHost.setEditable(stan);
                logowanie1.jTextFieldLogin.setEditable(stan);
            }
        });
    }

    @Override
    public void enableZapytanie(final boolean stan) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                zapytanie1.setVisible(stan);
            }
        });
    }

    @Override
    public void enableZadanie(final boolean stan) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                zadania1.setVisible(stan);
            }
        });
    }

    @Override
    public void alert(String komunikat) {
        JOptionPane.showMessageDialog(this, komunikat, getLocalized("alert.text") /*"Ostrzeżenie" */, JOptionPane.WARNING_MESSAGE);

    }

    @Override
    public void errorInfo(String komunikat) {
        JOptionPane.showMessageDialog(this, komunikat, getLocalized("errorInfo.text"), JOptionPane.ERROR_MESSAGE);
    }

    @Override
    public boolean tryAgain(String komunikat) {
        final String[] options = {getLocalized("tryAgain.yes"), getLocalized("tryAgain.no")};
        int result = JOptionPane.showOptionDialog(this, komunikat, getLocalized("tryAgain.question"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
        switch (result) {
            case JOptionPane.YES_OPTION:
                return true;
            case JOptionPane.NO_OPTION:
                return false;
            default:
                throw new IllegalStateException();
        }
    }

    @Override
    public void podlaczKontrolerLogowania(final LoginControler kon) {
        
        java.awt.EventQueue.invokeLater(new Runnable() {
           
            @Override
            public void run() {                
                addWindowListener(new AppCloseAction(kon,new Runnable() {

                    @Override
                    public void run() {
                        perfTimer.cancel();
                    }
                }));
                logowanie1.jButtonZaloguj.addActionListener(kon);
                logowanie1.jButtonWyloguj.addActionListener(kon);
                kon.actionPerformed(new ActionEvent(this, 0, LoginControler.CONNECTED_GUI));

            }
        });
    }

    @Override
    public void podlaczKontrolerZadan(final TaskControler kon) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                zadania1.jButtonZadanie.addActionListener(kon);
            }
        });
    }

    @Override
    public void podlaczKontrolerZapytan(final QueryControler kon) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {                
                zapytanie1.jButtonZapytanie.addActionListener(kon);
            }
        });
    }

    @Override
    public void setWynikZapytania(final List<Task> wynik) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                zapytanie1.setResults(wynik);                
                
            }
        });
    }

    public static String getLocalized(String id) {
        return rb.getString(id);
    }

    @Override
    public String podajZlokalizowane(String id) {
        return getLocalized(id);
    }

    @Override
    public void credentialsNecessary(boolean neccessary) {
        setCredentialsVisibility(neccessary);
    }
}
